const { BtnModel, jobAndBtnModel } = require('../model/index')
//添加按钮权限
exports.addBtn = async (req,res)=>{
    const { ButtonName , ButtonValue , ButtonContent } = req.body;
    if(!ButtonName || !ButtonValue || !ButtonContent) {
        return res.status(400).json({
            code:400,
            data:null,
            message:'参数不全'
        })
    }
    let result1 = await BtnModel.findOne({ButtonName})
    let result2 = await BtnModel.findOne({ButtonValue})
    if(result1 || result2) {
        return res.status(400).json({
            code:400,
            data:null,
            message:'按钮已存在'
        })
    } 
    try {
        let dbBack = new BtnModel({
            ButtonName,
            ButtonValue,
            ButtonContent
        })
        await dbBack.save()
        return res.status(200).json({
            code:200,
            data:null,
            message:'添加成功'
        })
    } catch (error) {
        return res.status(500).json({
            code:500,
            data:null,
            message:'添加失败'
        })
    }
    
}
//获取按钮列表
exports.getBtnList = async (req,res)=>{
    const { pageNo=1 , pageSize=5 } = req.query;
    console.log(pageNo,pageSize);
    try {
        let result = await BtnModel.find().skip((pageNo-1)*pageSize).limit(pageSize)
        let total = await BtnModel.find().count()
        return res.status(200).json({
            code:200,
            data:{
                list:result,
                total
            },
            message:'获取成功'
        })
    } catch (error) {
        return res.status(500).json({
            code:500,
            data:null,
            message:'获取失败'
        })
    }
}
//获取按钮详情
exports.getBtnDetail = async (req,res)=>{
    let id = req.query.id
    if(!id) {
        return res.status(400).json({
            code:400,
            data:null,
            message:'参数不全'
        })
    }
    try {
        let result = await BtnModel.findById(id)
        return res.status(200).json({
            code:200,
            data:result,
            message:'获取成功'
        })
    } catch (error) {
        return res.status(500).json({
            code:500,
            data:null,
            message:'获取失败'
        })
        
    }
}
//修改按钮
exports.updateBtn = async (req,res)=>{
    let id = req.query.id
    let {ButtonName,ButtonValue,ButtonContent} = req.body
    let updateAt = Date.now()
    if(!id) {
        return res.status(400).json({
            code:400,
            data:null,
            message:'参数不全'
        })
    }
    try {
        await BtnModel.findByIdAndUpdate(id,{ ButtonName,ButtonContent,ButtonValue , UpdateAt:updateAt })
        return res.status(200).json({
            code:200,
            data:null,
            message:'修改成功'
        })
    } catch (error) {
        return res.status(500).json({
            code:500,
            data:null,
            message:'修改失败'
        })
    }
}
//删除按钮
exports.deleteBtn = async (req,res)=>{
    let id = req.query.id
    if(!id) {
        return res.status(400).json({
            code:400,
            data:null,
            message:'参数不全'
        })
    }
    try {
        let result = await jobAndBtnModel.find({btnId:id})
        if(result.length>0) {
            return res.status(400).json({
                code:400,
                data:null,
                message:'该按钮已被使用，无法删除'
            })
        }
        await BtnModel.findByIdAndDelete(id)
        return res.status(200).json({
            code:200,
            data:null,
            message:'删除成功'
        })
    }catch(err) {
        return res.status(500).json({
            code:500,
            data:null,
            message:'删除失败'+err
        })
    }
}